﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using WebApplicationRating.DB.model;

namespace WebApplicationRating.Views
{
    public partial class UserImageRatings : System.Web.UI.Page
    {


        static string prevPage = String.Empty;


        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                try
                {
                    prevPage = Request.UrlReferrer.ToString();
                }
                catch
                {
                }
            }


            //if possible userid could not be in sync perform select query to validate by mgid
            //could just use string but if toggle to int know it is really a number

            int curUserId = SharedItems.Field2; //Convert.ToInt32(WebConfigurationManager.AppSettings["UserID"].ToString());
            String pusr = Convert.ToString(curUserId);
            String mgid = Request.QueryString["MGID"];
            int curImageId = Convert.ToInt32(mgid);
            
            tbMGID.Text = mgid;
            lblMessageM.Text = " imageid "; //+ mgid;
            tbPUSR.Text = pusr;
            lblMessageU.Text = " userid "; //+ pusr;

            //fetch what user uploaded image
            //this will be used to disable the hyperlink if the present user is equal
            //try
            //{
            try {
                using (imageEntities ctx = new imageEntities())
                {

                    UserImage u = (from ur in ctx.UserImages
                                   //join urp in ctx.UserImageRatings on //ur.imageid equals urp.imageid
                                   where (ur.imageid == curImageId) && ur.userid == curUserId
                                   select ur).First();


                    lblMessage.Text = "Can not RATE your own IMAGE. userid=" + Convert.ToString(u.userid);
                    //RadioButtonList.Enabled = false;
                    btnSave.Enabled = false;


                }
            }
            catch
            {


                //RadioButtonList.Enabled = true;
                btnSave.Enabled = true;

                //if possible userid could not be in sync perform select query to validate by mgid
                //could just use string but if toggle to int know it is really a number
                int curUserIdL = SharedItems.Field2;  //Convert.ToInt32(WebConfigurationManager.AppSettings["UserID"].ToString());
                String pusrL = Convert.ToString(curUserIdL);
                String mgidL = Request.QueryString["MGID"];
                int curImageIdL = Convert.ToInt32(mgidL);


                try
                {
                    using (imageEntities ctx = new imageEntities())
                    {

                        UserImageRating u = (from ur in ctx.UserImageRatings
                                             //join urp in ctx.UserImageRatings on //ur.imageid equals urp.imageid
                                             where (ur.imageid == curImageIdL) && ur.userid == curUserIdL
                                             select ur).First();

                        rblRating.Text = Convert.ToString(u.rating);
                        lblMessage.Text = "Previous Rating found : ) your userid=" + Convert.ToString(u.userid);

                    }
                }
                catch
                {
                }

            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            //if possible userid could not be in sync perform select query to validate by mgid
            //could just use string but if toggle to int know it is really a number
            int curUserIdL = SharedItems.Field2; //Convert.ToInt32(WebConfigurationManager.AppSettings["UserID"].ToString());
            String pusrL = Convert.ToString(curUserIdL);
            String mgidL = Request.QueryString["MGID"];
            int curImageIdL = Convert.ToInt32(mgidL);

            try
            {
                using (imageEntities ctx = new imageEntities())
                {

                    UserImageRating u = (from ur in ctx.UserImageRatings
                                   //join urp in ctx.UserImageRatings on //ur.imageid equals urp.imageid
                                   where (ur.imageid == curImageIdL) && ur.userid == curUserIdL
                                   select ur).First();

 
                    u.userid = curUserIdL;
                    u.imageid = curImageIdL;
                    //u.rating = Convert.ToInt32(DropDownList1.Text); //DropDownList; //TO DO set to list value
                    u.rating = Convert.ToInt32(rblRating.Text); //RadioList Selection
                    u.rating_date = DateTime.Now;

                    ctx.SaveChanges();
                    lblMessage.Text = "Rating Updated = " + u.rating + " !";

                    rblRating.Text = Convert.ToString(u.rating);
                    //already hardset GridView2.DataSource = SqlDataSourceRatings0;
                    GridView2.DataBind();
                    GridView1.DataBind();

                }
            }
            catch
            {
 
                lblMessage.Text = "Could not find a previous rating.";
                //Add
                try
                {
                    using (imageEntities ctx = new imageEntities())
                    {

                        UserImageRating uir = new UserImageRating();
                        uir.userid = curUserIdL;
                        uir.imageid = curImageIdL;
                        uir.rating = Convert.ToInt32(rblRating.Text); //TO DO set to list value
                        uir.rating_date = DateTime.Now;

                        if (uir.imageid > 0 && uir.userid > 0 && uir.rating > 0)
                        {
                            ctx.AddToUserImageRatings(uir);
                            int result = ctx.SaveChanges();

                            rblRating.Text = Convert.ToString(uir.rating);
                            //already hardset GridView2.DataSource = SqlDataSourceRatings0;
                            GridView2.DataBind();
                            GridView1.DataBind();

                            if (result > 0)
                                lblMessage.Text = "Rating Assigned to imageid = " + uir.rating;
                            else
                                lblMessage.Text = "Error Assigning Rating!";

                        }
                        else
                        {
                            lblMessage.Text = "Not Enough Information Provided.";
                        }
                    } //end using
                } //end try
                catch
                {
                    lblMessage.Text = "Rating Could not Be Assigned!";
                }



            }
            //End Save Logic
        }//end buttonSave

        protected void btnBack_Click(object sender, EventArgs e)
        {

            Response.Redirect(prevPage);

        }

        protected void ReviewAddBtn_Click(object sender, EventArgs e)
        {
        }

        

    }
}